package com.tester.zc.test001.first;

import java.lang.reflect.Field;

/**
 * 题目1：person 类有属性name(String)、age(Integer)、address(String),请使用反射设置name:jason,age:18,address:guang zhou China
 *
 * @Author zhoucan
 * @Date 2022/12/4
 */
public class ReflectionTest {

    public static void main(String[] args) throws Exception{
        //获取class对象
        Class<Person> personClass = Person.class;
        //获取对象
        Person person = personClass.newInstance();
        //获取属性
        Field name = personClass.getDeclaredField("name");
        //忽略修饰符
        name.setAccessible(true);
        name.set(person,"jason");

        Field age = personClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(person,18);

        Field address = personClass.getDeclaredField("address");
        address.setAccessible(true);
        address.set(person,"guang zhou China");
    }
}
